//
//  RoomInventory.cpp
//  Dungeons and Princesses
//
//  Created by John Grange on 11/7/11.
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#include <iostream>
#include "RoomInventory.h"

RoomInventory::RoomInventory() : Inventory(){

    listOfMonstersInRoom = new std::vector<Monster*>;
    numOfMonsters = 0;
}

void RoomInventory::add(Base* objectToAdd) {

    if (objectToAdd->getObjectType() == "Monster") {

        Monster * monsterToAdd = (Monster*)objectToAdd;
        listOfMonstersInRoom->push_back(monsterToAdd);
        ++numOfMonsters;
    }
    else {
        Inventory::add(objectToAdd);
    }
}


const std::vector<Monster *> * RoomInventory::getListOfMonsters() const {

#warning I don't like this code, I want this updated when adding and removing.

    std::vector<Monster*> *tmpInventory = new std::vector<Monster*>;

    //long size = listOfMonstersInRoom->size();

    for (int i = 0; i < listOfMonstersInRoom->size(); i++) {

        if (!listOfMonstersInRoom->at(i)->isDead()) {
            tmpInventory->push_back(listOfMonstersInRoom->at(i));
        }
    }
    return tmpInventory;
}

Monster * RoomInventory::getMonster(int objectID) {


    for (int i = 0; i < listOfMonstersInRoom->size(); i++) {

        if (listOfMonstersInRoom->at(i)->getObjectID() == objectID) {

            return listOfMonstersInRoom->at(i);
        }
        else {
            return NULL;
        }
    }

    return NULL;

}



void RoomInventory::saveRoomInventory(ostream& out)
{
    Inventory::saveInventory(out);
    out << numOfMonsters << endl;
    if(numOfMonsters > 0)
    {
        for(int i = 0; i < numOfMonsters; ++i)
        {
            out << *(listOfMonstersInRoom->at(i));
        }
    }
}

void RoomInventory::loadRoomInventory(istream& in)
{
    Inventory::loadInventory(in);
    Monster *m = new Monster();
    char data[MAX] = ""; //to hold line
    in.getline(data,MAX - 1, '\n');
    int tempNumOfMonsters = atoi(data);
    if(tempNumOfMonsters > 0)
    {
        for(int i = 0; i < tempNumOfMonsters; ++i)
        {
            in >> *m;
            listOfMonstersInRoom->push_back(m);
        }
    }
}
